package main

import (
	"fmt"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/disk"
	host2 "github.com/shirou/gopsutil/host"
	"github.com/shirou/gopsutil/load"
	"github.com/shirou/gopsutil/mem"
)

//采集CPU相关信息
func getCpuInfo() *fyne.Container {
	cpuInfos, err := cpu.Info()
	if err != nil {
		panic(err)
	}
	cc := container.NewGridWithColumns(len(cpuInfos))
	for _, ci := range cpuInfos {
		cc.Objects = append(cc.Objects, ListSlice(strings.Split(fmt.Sprint(ci), ",")))
	}
	return cc
	// for {
	// 	percent, _ := cpu.Percent(time.Second, false)
	// return	fmt.Sprintf("cpu percent :%v\n", percent) //cpu使用率
	// }
}

//获取CPU负载信息：
func getCpuLoad() string {
	info, _ := load.Avg()
	return fmt.Sprintf("load :%v\n", info)
}

//内存
func getMemInfo() string {
	memInfo, _ := mem.VirtualMemory()
	return fmt.Sprintf("mem info :%v\n", memInfo)
}

//Host
func getHostInfo() string {
	hinfo, _ := host2.Info()
	return fmt.Sprintf("host info:%s uptime:%v boottime:%v\n", hinfo, hinfo.Uptime, hinfo.BootTime)
}

//Disk 信息
func getDiskInfo() string {
	parts, err := disk.Partitions(true)
	if err != nil {
		panic(err)
	}
	for _, part := range parts {
		fmt.Printf("part:%v\n", part.String())
		diskInfo, _ := disk.Usage(part.Mountpoint)
		return fmt.Sprintf("disk info:used :%v free:%v\n", diskInfo.UsedPercent, diskInfo.Free)
	}

	ioStat, _ := disk.IOCounters()
	// for k, v := range ioStat {
	// 	fmt.Printf("%v,%v\n", k, v)
	// }
	return fmt.Sprint(ioStat)
}
